﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace SportingGoodsExhibitionSystem
{
    /// <summary>
    /// AdminFunctionOne.xaml 的交互逻辑
    /// </summary>
    public partial class AdminFunctionOne : Page
    {
        public AdminFunctionOne()
        {
            InitializeComponent();
        }
        string strCon = Properties.Resources.strCON;


        public DataTable getDataFromVacationByStatus()
        {
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select studentId as '学号',studentNa as '姓名', studentCollege as '学院' , reason as '请假原因', data as '申请时间', time as '申请请假开始日期',timeend as '请假结束日期' from vacation where status=@sta", con);
            cmd.Parameters.Add("@sta", SqlDbType.Int, 10).Value = 2;
            SqlDataAdapter read = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            read.Fill(dt);
            return dt;
        }


        public Boolean updateInfo(int num)
        {

            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("update vacation set status=@sta where studentId=@id and status=@st", con);
            cmd.Parameters.Add("@id", SqlDbType.Int, 10).Value = num;
            cmd.Parameters.Add("@sta", SqlDbType.Int, 10).Value = 1;
            cmd.Parameters.Add("@st", SqlDbType.Int, 10).Value = 2;
            //ExecuteNonQuery指令执行,更新操作
            cmd.ExecuteNonQuery();
            cmd = new SqlCommand("select leaveTimes from [studentInfo] where studentNumber=@id", con);
            cmd.Parameters.Add("@id", SqlDbType.VarChar, 50).Value = num;
            SqlDataAdapter read = new SqlDataAdapter(cmd);
            DataSet dataset = new DataSet();
            read.Fill(dataset);
            DataTable dt = dataset.Tables[0];
            DataRowCollection row = dt.Rows;
            DataRow dataRow = row[0];
            int tem = Convert.ToInt32(dataRow["leaveTimes"]);
            cmd = new SqlCommand("update studentInfo set leaveStatus=@sta  where studentNumber=@id", con);
            cmd.Parameters.Add("@id", SqlDbType.VarChar, 50).Value = num;
            cmd.Parameters.Add("@sta", SqlDbType.VarChar, 50).Value = 1;
            //ExecuteNonQuery指令执行,更新操作
            cmd.ExecuteNonQuery();
            cmd = new SqlCommand("update studentInfo set leaveTimes=@times where studentNumber=@id", con);
            cmd.Parameters.Add("@id", SqlDbType.VarChar, 50).Value = num;
            cmd.Parameters.Add("@times", SqlDbType.Int, 10).Value = tem + 1;
            cmd.ExecuteNonQuery();
            con.Close();
            return true;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable dt = getDataFromVacationByStatus();
            dataGrid.ItemsSource = dt.DefaultView;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dataGrid.SelectedItem;
            if (updateInfo(Convert.ToInt32(row["学号"])))
            {
                MessageBox.Show("ok");
                DataTable dt = getDataFromVacationByStatus();
                dataGrid.ItemsSource = dt.DefaultView;
            }
            else
            {
                MessageBox.Show("error");
            }
        }

        public Boolean deleteInfo(int num)
        {

            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("update vacation set status=@sta where studentId=@id and status=@st", con);
            cmd.Parameters.Add("@id", SqlDbType.Int, 10).Value = num;
            cmd.Parameters.Add("@sta", SqlDbType.Int, 10).Value = 0;
            cmd.Parameters.Add("@st", SqlDbType.Int, 10).Value = 2;
            //ExecuteNonQuery指令执行,更新操作
            cmd.ExecuteNonQuery();
            cmd = new SqlCommand("update studentInfo set leaveStatus=@sta where studentNumber=@id", con);
            cmd.Parameters.Add("@id", SqlDbType.VarChar, 50).Value = num;
            cmd.Parameters.Add("@sta", SqlDbType.VarChar, 50).Value = 0;
            //ExecuteNonQuery指令执行,更新操作
            cmd.ExecuteNonQuery();
            con.Close();
            return true;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dataGrid.SelectedItem;
            if (deleteInfo(Convert.ToInt32(row["学号"])))
            {
                MessageBox.Show("ok");
                DataTable dt = getDataFromVacationByStatus();
                dataGrid.ItemsSource = dt.DefaultView;
            }
            else
            {
                MessageBox.Show("error");
            }
        }
    }
}
